"""add_ai_error_tracking_to_import_record

Revision ID: 20251011_ai_error_tracking
Revises: 7a9659384fc4
Create Date: 2025-10-11 14:30:00.000000

Add error tracking fields to ImportRecord for AI extraction failures:
- ai_extraction_error: stores error message when AI tagging fails
- ai_extraction_attempts: tracks number of retry attempts

This resolves PRD issue: "batch_extract_ai_tags() 中AI提取失败仅打印错误，未记录到数据库"
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '20251011_ai_error_tracking'
down_revision = '7a9659384fc4'
branch_labels = None
depends_on = None


def upgrade() -> None:
    """Add AI error tracking columns to import_records table."""
    # Add ai_extraction_error column to store error messages
    op.add_column(
        'import_records',
        sa.Column(
            'ai_extraction_error',
            sa.Text(),
            nullable=True,
            comment='Error message when AI tag extraction fails'
        )
    )

    # Add ai_extraction_attempts column to track retry count
    op.add_column(
        'import_records',
        sa.Column(
            'ai_extraction_attempts',
            sa.Integer(),
            nullable=False,
            server_default='0',
            comment='Number of AI extraction attempts (for monitoring)'
        )
    )


def downgrade() -> None:
    """Remove AI error tracking columns from import_records table."""
    op.drop_column('import_records', 'ai_extraction_attempts')
    op.drop_column('import_records', 'ai_extraction_error')
